<?php

    //验证session
    session_start();
    if(!isset($_SESSION['username'])){
        header("index.php");
    }

    include("../config/config.php");

////////////////////////////////////////////////////控制器
    if(!isset($_POST['handle'])){
        $handle=$_GET['handle'];
    }
    else{
        $handle=$_POST['handle'];
    }

    //权限验证
    //规定修改各方法所属权限
    switch ($handle) {
//---------------登陆操作-----------------//
        case 'login':
            //登陆
            echo login($dbh);
            exit;
        case 'logout':
            //登出
            logout();
            exit;   
//---------------修改类方法---------------//
        case 'mark_apply':
        case 'sava_frame_data':
        case 'sava_module_data':
            $qx_name="modify";
            break;
//---------------查看类方法---------------//
        case 'frame_data': 
        case 'module_data':
        case 'get_apply':
            $qx_name="check";
            break;
//---------------删除类方法---------------//
        case 'dele_apply':
        case 'dele_frame_data':   
        case 'dele_module_data':
            $qx_name="dele";
            break;
//---------------------------------------//
        default:
            exit;
            break;
    }

    //验证权限
   if(check_qx($qx_name,$dbh)){
        switch ($handle) {
            case 'dele_apply':
                //删除申请数据
                echo json_encode(dele_apply($dbh));
                break;
            case 'mark_apply':
                //标记申请数据
                echo json_encode(mark_apply($dbh));
                break;
            case 'get_apply':
                //获取申请数据
                echo json_encode(get_apply($dbh));
                break;
            case 'frame_data':
                //获取框架数据
                echo json_encode(frame_data($dbh));
                break;
            case 'module_data':
                //获取模块数据
                echo json_encode(module_data($dbh));
                break;
            case 'sava_module_data':
                //保存模块数据
                echo json_encode(sava_module_data($dbh));
                break;
            case 'sava_frame_data':
                //保存框架数据
                echo json_encode(sava_frame_data($dbh));
                break;
            case 'dele_module_data':
                //删除模块数据
                echo json_encode(dele_module_data($dbh));
                break;
            case 'dele_frame_data':
                //删除哭框架数据
                echo json_encode(dele_frame_data($dbh));
                break;
            default:
                break;
                exit;
        }
   }
    

////////////////////////////////////////////////////方法
//登陆
function login($dbh){
    
    if(!isset($_SESSION["authnum_session"])){
        return "0||系统错误,请稍后再试！";
    }
    if(strtolower($_SESSION["authnum_session"])!=strtolower($_POST["validate"])){
        session_destroy();//销毁验证码
        return "0||验证码错误！";
    }

    $user_id=$_POST["username"];
    $password=$_POST["password"];
    $password=md5($password);
    $rs = $dbh->query("SELECT * FROM user WHERE user_id ='".$user_id."' and password='".$password."'");
    $result_arr = $rs->fetchAll();
    if($result_arr ){
        $_SESSION['username']=$user_id;
        // $_SESSION['password']=$password;
        return "1||";
    }
    else{
        return "0||账号或密码错误！";
    }
}
//登出
function logout(){
   
    unset($_SESSION['username']);
    // unset($_SESSION['username']);
    header("location:index.php");
}
//保存或者添加框架数据
function sava_frame_data($dbh){
    //修改
    if(trim($_POST['id'])){
        $count = $dbh->exec("UPDATE frame_data SET module='".$_POST['module']."' ,module_name='".$_POST['title']."',type='".$_POST['type']."',status='".$_POST['status']."' ,url='".$_POST['url']."' ,data_order='".$_POST['data_order']."' ,time=NOW() WHERE id='".$_POST['id']."'");
    }
    //添加
    else{
        if(!$_POST['belong']){
            $lavel1=$dbh->query("SELECT * FROM frame_data WHERE lavel='1'")->fetchAll();
            $max_id=1;
            foreach ($lavel1 as  $value) {
                if(substr($value['module'],strlen($value['module'])-1)>$max_id) {
                    $max_id=substr($value['module'],strlen($value['module'])-1);
                }
            }
            $max_id++;
            $count = $dbh->exec("INSERT INTO frame_data SET  module='module_".$max_id."' ,module_name='".$_POST['title']."',lavel='1',type='".$_POST['type']."',status='".$_POST['status']."' ,url='".$_POST['url']."' ,data_order='".$_POST['data_order']."' ,time=NOW()");
        }
        else{
            $lavel2=$dbh->query("SELECT * FROM frame_data WHERE belong='".$_POST['belong']."' AND lavel='2'")->fetchAll();
            $max_id=1;
            foreach ($lavel2 as  $value) {
                if(substr($value['module'],strlen($value['module'])-1)>$max_id) {
                    $max_id=substr($value['module'],strlen($value['module'])-1);
                }
            }
            $max_id++;
            $count = $dbh->exec("INSERT INTO frame_data SET  module='".$_POST['belong']."_".$max_id."' ,module_name='".$_POST['title']."',belong='".$_POST['belong']."',lavel='2',type='".$_POST['type']."',status='".$_POST['status']."' ,url='".$_POST['url']."' ,data_order='".$_POST['data_order']."' ,time=NOW()");
        }
    }
    return result("save_module",$count,"保存",$dbh);
}  

//保存或者添加模块数据
function sava_module_data($dbh){
    //修改
    if(trim($_POST['id'])){
        $count = $dbh->exec("UPDATE module_data SET module='".$_POST['module']."' ,title='".$_POST['title']."',img='".$_POST['img']."',content='".$_POST['content']."' ,url='".$_POST['url']."' ,data_order='".$_POST['data_order']."' ,time=NOW() WHERE id='".$_POST['id']."'");
    }
    //添加
    else{
        $count = $dbh->exec("INSERT INTO module_data SET module='".$_POST['module']."' ,title='".$_POST['title']."',img='".$_POST['img']."',content='".$_POST['content']."' ,url='".$_POST['url']."' ,data_order='".$_POST['data_order']."' ,time=NOW()");
    }
    return result("save_module",$count,"保存",$dbh);
}  

//标记申请数据
function mark_apply($dbh){
    $count = $dbh->exec("UPDATE apply_data set mark =1 WHERE id=".$_POST['id']);
    return result("mark",$count,"标记",$dbh);
}
//删除申请数据
function dele_apply($dbh){
    $count = $dbh->exec("DELETE FROM apply_data WHERE id=".$_POST['id']);
    return result("dele",$count,"删除",$dbh);
}

//删除模块数据
function dele_module_data($dbh){
    $count = $dbh->exec("DELETE FROM module_data WHERE id=".$_POST['id']);
    return result("dele",$count,"删除",$dbh);
}
//删除框架数据
function dele_frame_data($dbh){
    $count = $dbh->exec("DELETE FROM frame_data WHERE id=".$_POST['id']);
    return result("dele",$count,"删除",$dbh);
}

//获取申请数据
function get_apply($dbh){
    $rs = $dbh->query("SELECT * FROM apply_data ORDER BY `time` DESC")->fetchAll();
    return $rs;
}
//获取模块数据
function module_data($dbh){
    $rs = $dbh->query("SELECT * FROM module_data WHERE module='".$_POST['module']."' ORDER BY `data_order`")->fetchAll();
    return $rs;
}
//获取框架数据
function frame_data($dbh){
    if($_POST['lavel']=="1"){
        $rs = $dbh->query("SELECT * FROM frame_data WHERE lavel='1' ORDER BY `data_order`")->fetchAll();
    }
    elseif($_POST['lavel']=="2"){
        $lavel1 = $dbh->query("SELECT * FROM frame_data WHERE lavel='1'")->fetchAll();
        $lavel2 = $dbh->query("SELECT * FROM frame_data WHERE lavel='2' ORDER BY module, `data_order`")->fetchAll();
        foreach ($lavel2 as $key => $value2) {
            foreach ($lavel1 as $value1) {
                if($value2['belong']==$value1['module']){
                    $lavel2[$key]['belong_name']=$value1['module_name'];
                }
            }
        }
        $rs=$lavel2;
    }
    return $rs;
}
//结果输出
function result($function,$rs,$state,$dbh){
    if($rs){
        return array("code"=>"done","msg"=>$state."成功！");
    }
    else{
        $error_message=$dbh->errorInfo();
        $logfile=fopen("log/log.log","a+");
        fwrite($logfile,$function.":".$error_message[2]."----".date("y-m-d H:i:s",time())."\r\n");
        fclose($logfile);
        return array("code"=>"erro","msg"=>"数据库操作过程出错");
    }
}
//核对权限
function check_qx($qx_name,$dbh){
    $this_userid = $dbh->query("SELECT id FROM user WHERE user_id='".$_SESSION['username']."' AND qx LIKE '%".$qx_name."%'")->fetchAll();
    if($this_userid){
        return true;
    }
    else{
        return false;
    }
}